package com.marchsoft.musicalstore.service;
import com.marchsoft.musicalstore.entity.SelClassAble;
import com.marchsoft.musicalstore.entity.StuCourse;
import com.marchsoft.musicalstore.entity.User;
import com.marchsoft.musicalstore.exception.CumtomException;
import com.marchsoft.musicalstore.repository.SelClassAbleRepository;
import com.marchsoft.musicalstore.repository.StuCourseRepository;
import com.marchsoft.musicalstore.util.MyUntil;
import com.marchsoft.musicalstore.util.RequestToJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import javax.xml.crypto.Data;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 云
 * Date: 2019-11-22
 * Time: 19:36
 *
 */
@Service
public class StuCourseSer<list> {

    @Autowired
    StuCourseRepository stuCourseRepository;
    @Autowired
    SelClassAbleSer selClassAbleSer;
    @Autowired
    MyUntil myUntil;
    @Autowired
    SelClassAbleRepository selClassAbleRepository;

    //通过id获取个人课程信息
    public List<com.marchsoft.musicalstore.entity.StuCourse> getPerCourse(int id){
        return  stuCourseRepository.findById(id);
    }

    //通过时间和id获取今日课程信息
    public List<com.marchsoft.musicalstore.entity.StuCourse> getPerTodayCourse(int id, Data PickClassData){
        return  stuCourseRepository.findByIdAndPickClassData(id,PickClassData);
    }

    /**
     * 源通过学生id和班课id获取其班课
     * @param stuId
     * @param ClaId
     * @return
     */
    public List<StuCourse> getMyComCourseByStuId(int stuId, int ClaId){
        List<StuCourse> byStuIdAndPickClassId = stuCourseRepository.findByStuIdAndPickClassId(stuId, ClaId);
        return byStuIdAndPickClassId;
    }

    /**
     * 源保存一个对象
     * @param stuCourse
     * @return
     */
    public StuCourse saveConMyCourse(StuCourse stuCourse){
        StuCourse save = stuCourseRepository.save(stuCourse);
        return save;
    }



    /**
     * 源写的公共班课的选取
     * @param id
     * @param session
     * @return
     */
    public RequestToJson addToComMyCourse(int id, HttpSession session){
        Optional<SelClassAble> oneClass = selClassAbleSer.getOneClass(id);
        SelClassAble selClassAble = oneClass.get();
        if(selClassAble.getLimitPeonum()<=selClassAble.getPassPeonum()){//判断人数是否满了
            throw new CumtomException("该课人数已满");
        }
        User user = (User)session.getAttribute("user");
        List<StuCourse> myComCourseByStuId = this.getMyComCourseByStuId(id, user.getId());
        if(myComCourseByStuId.size()>0){
            throw new CumtomException("你之前已经加入购物车或买过了哦！");
        }
        StuCourse stuCourse = new StuCourse();
        stuCourse.setStuId(user.getId());
        stuCourse.setClassHour(selClassAble.getClassHour());
        stuCourse.setPickClassData(new Date());
        stuCourse.setPickClassId(id);
        StuCourse stuCourse1 = this.saveConMyCourse(stuCourse);
        return new RequestToJson(0,"加入购物车成功");
    }


    /**
     * 源用于返回日历中查询当天课程,这个算法其实很有问题,有的东西没考虑
     * @param time
     * @param week
     * @param session
     * @return
     */
    public List<SelClassAble> getDayClassBytime(Integer time, Integer week, HttpSession session){
        if((time<=9999999)||(time>=99999999)){//判断时间格式是否符合规范
            throw new CumtomException("时间格式不符！");
        }
        User user = (User)session.getAttribute("user");
        List<SelClassAble> selClassAbles = selClassAbleRepository.queryAllByStuId(user.getId(), week);
        Date date = myUntil.getDate(time);
        selClassAbles.removeIf(ele->{
            if(date.compareTo(ele.getCourseDate())<=0){//如果时间比开课时间早，直接过滤
                return true;
            }
            long twoDateBetweenDay = myUntil.getTwoDateBetweenDay(ele.getCourseDate(), date);
            long classtime = twoDateBetweenDay/7;//算出中间应该有多少节课
            if(classtime>ele.getClassHour()){
                return true;
            }
            return false;
        });
        return selClassAbles;
    }

    /**
     * 查询学生当天课程
     * @param id 选课id
     * @param date 现在时间
     * @param type 课程类型
     * @return
     */
    public List<com.marchsoft.musicalstore.entity.SelfTodayClass> getOneClass(int id, Date date,int type){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        return stuCourseRepository.queryAllByClassType(id,w,type);
    }

}
